﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.iss.inventory.CustomModels;
using com.iss.inventory.Entity;
using com.iss.inventory.Controllers;
using com.iss.inventory.BusinessLogic;

namespace Staionery_Inventory.Presentation.Disbursement
{
    public partial class AllDisbursementList : System.Web.UI.Page
    {
        StationeryRetrievalController srfControl = new StationeryRetrievalController();
        List<SRFDetail> srfDetailsList = new List<SRFDetail>();
        SearchController searchControl = new SearchController();
        customSRFDetailsModel customSRFModel;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["empID"] == null)
            {
                Response.Redirect("~/Presentation/login.aspx");
            }

            Label lbl = this.Master.FindControl("lblLogInName") as Label;
            if (lbl != null)
            {
                lbl.Text = Session["UserName"].ToString();
            }


            string prevURL;
            if (!IsPostBack)
            {
                //
                customSRFModel = new customSRFDetailsModel();
                //
                if (Request.UrlReferrer != null)
                {
                    ViewState["prevToSRFpage"] = Request.UrlReferrer;
                    prevURL = ViewState["prevToSRFpage"].ToString();
                }
                else
                {
                    prevURL = "abc";
                }
                if ((!prevURL.Contains("SRFList.aspx")) || ((Request.QueryString["srfID"] == null) && prevURL.Contains("SRFList.aspx"))) //need to freeze srf form once it is create for a week, cannot create more than one for a week 
                {
                    if (srfControl.getIncompleteRequestsforSRF() != null)
                    {
                        customSRFModel = srfControl.getIncompleteRequestsforSRF();
                        srfDetailsList = customSRFModel.srfDetailsList;
                      
                    }
                
                }
                else
                {
                    int srfID = 0; //need  to assign a default value
                    if (lblSrfIdValue.Text != "")
                    {
                        srfID = Convert.ToInt32(lblSrfIdValue.Text);
                    }
                    if (Request.QueryString["srfID"] != null)
                    {
                        srfID = Convert.ToInt32(Request.QueryString["srfID"].ToString());
                    }

                    SRF srf = srfControl.getSRF(srfID);
                    srfDetailsList.Clear();
                    foreach (SRFDetail srfd in srf.SRFDetails)
                    {
                        srfDetailsList.Add(srfd);
                    }
                   lblSrfIdValue.Text = srf.SRFID.ToString();
                  }
              
                var custom =from s in srfDetailsList
                           orderby s.DeptID
                             select new { ItemDescription = s.StationeryItem.Description, DepartmentName = s.Department.DeptName, NeededQty = s.QtyRequested, ActualQty = s.QtyRetrieved };
                gvSrfList.DataSource = custom.ToList<object>();
                gvSrfList.DataBind();
                ViewState["srfDetailsList"] = customSRFModel;
                 }
            else
            {
                customSRFModel = ViewState["srfDetailsList"] as customSRFDetailsModel;
                srfDetailsList = customSRFModel.srfDetailsList;
            }
        }
        protected void gvSrfList_DataBound(object sender, EventArgs e)
        {
        
            for (int rowIndex = gvSrfList.Rows.Count - 2; rowIndex >= 0; rowIndex--)
            {
                GridViewRow gvRow = gvSrfList.Rows[rowIndex];
                GridViewRow gvPreviousRow = gvSrfList.Rows[rowIndex + 1];
                for (int cellCount = 0; cellCount < 1; cellCount++)
                {
                    if (gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Text)
                    {
                        if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
                        {
                            gvRow.Cells[cellCount].RowSpan = 2;
                         //   gvRow.Cells[cellCount+1].RowSpan = 2;
                        }
                        else
                        {
                            gvRow.Cells[cellCount].RowSpan = gvPreviousRow.Cells[cellCount].RowSpan + 1;
                           // gvRow.Cells[cellCount+1].RowSpan = gvPreviousRow.Cells[cellCount+1].RowSpan + 1;
                        }
                        gvPreviousRow.Cells[cellCount].Visible = false;
                       // gvPreviousRow.Cells[cellCount+1].Visible = false;
                    }
                }
            }
            
            if(srfDetailsList.Count!=0)
            if(srfDetailsList[0].SRF != null)
            if(srfDetailsList[0].SRF.StatusforForm.Contains("Disbursed"))
            foreach (GridViewRow r in gvSrfList.Rows)
            {
                TextBox txt = r.FindControl("actualQty") as TextBox;
                txt.Enabled = false;
            }

        }

        protected void lbdepartmentName_Click(object sender, EventArgs e)
        {
            LinkButton lb = sender as LinkButton;
            Response.Redirect("~/Presentation/Disbursement/DisbursementList.aspx?department=" + lb.Text+"&strID="+lblSrfIdValue.Text);
        }
    }
}